package Servlets;

import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import DBConnectionPackage.*;

public class DataServlet extends HttpServlet{

	private ServletConfig config;
	//Setting JSP page
	String page="DataPage.jsp";

	public void init(ServletConfig config)
	  throws ServletException{
		 this.config=config;
	   }
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
		
                ConnectionFactory myFactory = ConnectionFactory.getFactory();
                Connection conn = myFactory.getConnection();
            
            
		PrintWriter out = response.getWriter();

		
		response.setContentType("text/html");
		List dataList=new ArrayList(); 
			try {
			 // Load the database driver
			Class.forName("com.mysql.jdbc.Driver");

                      
                        PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM products");
                        
			ResultSet rs = pstmt.executeQuery();
                        
			while (rs.next ()){
				//Add records into data list
				dataList.add(rs.getString("title"));
				dataList.add(rs.getString("synopsis"));
                                dataList.add(rs.getFloat("price"));
                                dataList.add(rs.getString("productID"));
			}
			rs.close ();
			
			}catch(Exception e){
			System.out.println("Exception is ;"+e);
			}
			request.setAttribute("data",dataList);
			//Disptching request
			RequestDispatcher dispatcher = request.getRequestDispatcher(page);
			if (dispatcher != null){
				dispatcher.forward(request, response);
			} 
	}
}